<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-1" />
  <title>Code Examples for Programming in Scala, Third Edition</title>
  <link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>

<div id="mainTitles"><h3>Code Examples for</h3><h2>Programming in Scala, Third Edition</h2></div>  <p><a href="../index.html">
    Return to chapter index
  </a></p>
  <h2>1 A Scalable Language</h2>

    <li>1.1 <a href="#sec1">A language that grows on you</a></li>
    <li>1.2 <a href="#sec2">What makes Scala scalable?</a></li>
    <li>1.3 <a href="#sec3">Why Scala?</a></li>
    <li>1.4 <a href="#sec4">Scala's roots</a></li>
    <li>1.5 <a href="#sec5">Conclusion</a></li>
  </ul>

  <h3><a name="sec1"></a>1.1 A language that grows on you</h3>

  <pre><hr>
  var capital = Map("US" -&gt; "Washington", "France" -&gt; "Paris")

  capital += ("Japan" -&gt; "Tokyo")

  println(capital("France")) 

<hr>
  def factorial(x: BigInt): BigInt = 
    if (x == 0) 1 else x * factorial(x - 1)

<hr>
  265252859812191058636308480000000

<hr>
  import java.math.BigInteger

  def factorial(x: BigInteger): BigInteger =
    if (x == BigInteger.ZERO)
      BigInteger.ONE
    else
      x.multiply(factorial(x.subtract(BigInteger.ONE)))

<hr>
  recipient ! msg

<hr>
  def receive = {
    case Msg1 =&gt; ... // handle Msg1
    case Msg2 =&gt; ... // handle Msg2
    // ...
  }

<hr>
  class ChecksumActor extends Actor {
    var sum = 0
    def receive = {
      case Data(byte) =&gt; sum += byte
      case GetChecksum(requester) =&gt;
        val checksum = ~(sum &amp; 0xFF) + 1
        requester ! checksum
    }
  }

<hr>
  </pre>
  <h3><a name="sec2"></a>1.2 What makes Scala scalable?</h3>

  <pre><hr>
val xs = 1 to 3
val it = xs.iterator
eventually { it.next() shouldBe 3 }

<hr>
  </pre>
  <h3><a name="sec3"></a>1.3 Why Scala?</h3>

  <pre><hr>
  // this is Java
  class MyClass {

      private int index;
      private String name;

      public MyClass(int index, String name) {
          this.index = index;
          this.name = name;
      }
  }

<hr>
  class MyClass(index: Int, name: String)

<hr>
  boolean nameHasUpperCase = false;  // this is Java
  for (int i = 0; i &lt; name.length(); ++i) {
      if (Character.isUpperCase(name.charAt(i))) {
          nameHasUpperCase = true;
          break;
      }
  }

<hr>
  val nameHasUpperCase = name.exists(_.isUpper) 

<hr>
  boolean nameHasUpperCase =   // This is Java 8
      name.chars().anyMatch(
          (int ch) -&gt; Character.isUpperCase((char) ch)
      );

<hr>
  s.exists(p) || s.exists(q) == s.exists(x =&gt; p(x) || q(x)) 

<hr>
  def f(x: String) = ...  

<hr>
  val x: HashMap[Int, String] = new HashMap[Int, String]()

<hr>
  val x = new HashMap[Int, String]()
  val x: Map[Int, String] = new HashMap() 

<hr>
  </pre>
  <h3><a name="sec4"></a>1.4 Scala's roots</h3>

  <h3><a name="sec5"></a>1.5 Conclusion</h3>


 <table>
 <tr valign="top">
 <td>
 <div id="moreinfo">
 <p>
 For more information about <em>Programming in Scala, Third Edition</em> (the "Stairway Book"), please visit:
 </p>
 
 <p>
 <a href="http://www.artima.com/shop/programming_in_scala_3ed">http://www.artima.com/shop/programming_in_scala_3ed</a>
 </p>
 
 <p>
 and:
 </p>
 
 <p>
 <a href="http://booksites.artima.com/programming_in_scala_3ed">http://booksites.artima.com/programming_in_scala_3ed</a>
 </p>
 </div>
 </td>
 <td>
 <div id="license">
 <p>
   Copyright &copy; 2007-2016 Artima, Inc. All rights reserved.
 </p>

 <p>
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 </p>

 <p style="margin-left: 20px">
   <a href="http://www.apache.org/licenses/LICENSE-2.0">
     http://www.apache.org/licenses/LICENSE-2.0
   </a>
 </p>

 <p>
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 </p>
 </div>
 </td>
 </tr>
 </table>

</body>
</html>
